// pages/more/more.js
var app = getApp();

Page({

    data: {
        currentTab: 0,
        inTheaters: [],
        comingSoon: [],
        TOP250: [],
        defaultImage: '/images/default-img.jpg',
    },

    onLoad(e) {
        var type = e.type;
        var that = this;
        wx.request({
          url: app.globalData.gBaseUrl + 'in_theaters',
          data: {
              start: 0,
              count: 12
          },
          method: 'GET',
          success:function(res) {
            console.log(res);
            that.setData({inTheaters:res.data.subjects});
          },
        });
        wx.request({
            url: app.globalData.gBaseUrl + 'coming_soon',
            data: {
                start: 0,
                count: 12
            },
            method: 'GET',
            success:function(res) {
              console.log(res);
              that.setData({comingSoon:res.data.subjects});
            },
          });
        wx.request({
            url: app.globalData.gBaseUrl + 'top250',
            data: {
                start: 0,
                count: 12
            },
            method: 'GET',
            success:function(res) {
              console.log(res);
              that.setData({TOP250:res.data.subjects});
            },
          });

        if (type == 'in_theaters') {
            that.setData({currentTab: 0});
        };
        if (type == 'coming_soon') {
            that.setData({currentTab: 1});             
        };
        if (type == 'top250') {
            that.setData({currentTab: 2});             
        }
    },

  //页签切换
  switchNav:function(e) {
    var type = e.currentTarget.dataset.current;
    console.log(type);
    if (type == this.data.currentTab) {
        return false
    } else {
        this.setData({currentTab: type})
    }
  },

    //海报获取失败时使用默认海报
    onError:function(e) {
        //   console.log(e)
          var index = e.currentTarget.dataset.index;
          var name = e.currentTarget.dataset.name;
          if (name == 'inTheaters') {
            var inTheaters = this.data.inTheaters;
            inTheaters[index].images.large = this.data.defaultImage;
            this.setData({inTheaters});
          };
          if (name == 'comingSoon') {
            var comingSoon = this.data.comingSoon;
            comingSoon[index].images.large = this.data.defaultImage;
            this.setData({comingSoon});
          };
          if (name == 'TOP250') {
            var TOP250 = this.data.TOP250;
            TOP250[index].images.large = this.data.defaultImage;
            this.setData({TOP250});
          };
      },
})